package com.yf.controller;

import java.io.IOException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yf.basic.Msg;


@Controller
@RequestMapping(value="/login")
public class UserController {
	@RequestMapping(value="",method=RequestMethod.GET)
	public String Login() {	
		return "login";
	}
	
	@RequestMapping(value="/error",method=RequestMethod.GET)
	public String error() {	
		return "error";	
	}
	
	@RequestMapping(value="/success",method=RequestMethod.GET)
	public String success() {	
		return "success";
	}
	
	@RequestMapping(value="/submit",method=RequestMethod.POST)
	@ResponseBody
	public Msg submit(@RequestParam("username")String username,@RequestParam("password")String password) throws IOException {
		//获的shiro主体
		Subject currentUser=SecurityUtils.getSubject();
		//若没有认证则进行认证
		if(currentUser.isAuthenticated()==false) {
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			try {
				currentUser.login(token);
			}catch(AuthenticationException e){
				return Msg.fail("登录失败");
			}
		}
		return Msg.success("登录成功");
    }  
}
